<?php

include('pagina.php');
include('database.php');
include('auth.php');
include('core.php');

// Verifica se tem permissao
validaPermissao(3);

// Verifica se houve submit
if (isset($_POST['submit']))
{
	query_bd("INSERT INTO supplies VALUES " .
	"(NULL, '{$_POST['name']}', '{$_POST['description']}', {$_POST['category']}, 0, {$_POST['value']}, ".
	($_POST['obs']? "'" . mysql_real_escape_string($_POST['obs']) . "')" : "NULL)"));

	$supply_id = mysql_insert_id(); //pega ultimo id inserido

	header( "Location: supplies.php?id={$supply_id}&msg=New supply item added to the system." );
	die();
}

// Inicia cabecalho da pagina
pagina_inicio('ParkSys - New Supply item');
adicionar_js('input-format.js');
pagina_head();

?>
<script type='text/javascript' language='JavaScript'>

function checkForm()
{
	if (document.supply_new.name.value.length == 0)
	{
		alert("The name field must not be empty.");
		return false;
	}
	else
		return true;
};

</script>
<?php

// Formulario para entrar com os dados do novo supply
echo "<form name=supply_new class=big_form method=POST action='supply_new.php' onSubmit='return checkForm()'>";
echo "<h1>New Supply Item</h1><br>";
echo "<label for=name>Item name:</label> <input type=TEXT id=name name=name value='' size=40 maxlength=50 /><br>";
echo "<label for=description>Description:</label> <input type=TEXT id=description name=description value='' size=40 maxlength=100 /><br>";
echo "<label for=category>Category:</category> <select id=category name=category>";

foreach ($SUPPLIES_CATEGORIES as $num => $name)
	echo "<option value={$num}>{$name}</option>";

echo "</select>";
echo "<label for=value>Price:</label> \$<input type=TEXT id=value name=value value='0.0' size=10 onChange='format_money(this)' /><br>";
echo "<label for=obs>Observations:</label><br><textarea id=obs name=obs cols=40 rows=5 />";
echo "</textarea>";
echo "<hr>";
echo "<input type=SUBMIT name=submit value='Submit' />";

echo "</form>";

// Finaliza pagina
pagina_fim();
?>
